package shape.maze;

import java.util.ArrayList;
import java.util.Random;

import processing.core.PApplet;
import processing.core.PVector;
import shape.Polyhedra;
import shape.PolyhedraFace;

public class Maze {

	private Cell[][] data;

	public Maze(Cell[][] data) {
		this.data = data;
	}

	public Cell[][] getMaze() {
		return data;
	}

	public ArrayList<Polyhedra> getPolyhedraRepresentation(float size,
			float origZ) {
		ArrayList<Polyhedra> rep = new ArrayList<Polyhedra>(data.length
				* data[0].length);
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[0].length; j++) {
				rep.add(data[i][j].getRepresentation(new PVector(j * size
						, i * size , origZ), size));
			}
		}
		return rep;
	}

	public String getStringRepresentation(int size) {
		StringBuffer sb = new StringBuffer();
		int i;
		char d = ' ';
		for (i = data.length - 1; i >= 0; i--) {
			for (int j = 0; j < data[0].length; j++) {
				d = ' ';
				if (data[i][j].up
						|| (data[i + 1][j].down && i < data.length - 1)) {
					d = '-';
				}
				sb.append('-');
				for (int k = 0; k < size; k++)
					sb.append(d);
			}
			sb.append(d);
			sb.append('\n');
			int j = 0;
			for (j = 0; j < data[0].length; j++) {
				d = ' ';
				if (data[i][j].left || (j > 0 && data[i][j-1].right)) {
					d = '|';
				}
				sb.append(d);
				for (int k = 0; k < size; k++)
					sb.append(' ');
			}
			d = ' ';
			if (data[i][j-1].right) {
				d = '|';
			}
			sb.append(d);
			sb.append('\n');
		}
		for (int j = 0; j < data[0].length; j++) {
			d = ' ';
			if (data[i + 1][j].down) {
				d = '-';
			}
			sb.append(d);
			for (int k = 0; k < size; k++)
				sb.append(d);
		}
		sb.append(d);
		return sb.toString();
	}

	public static class Cell {

		boolean up = false;
		boolean down = false;
		boolean left = false;
		boolean right = false;

		public Cell() {
		}

		public boolean wallUp() {
			return up;
		}

		public boolean wallDown() {
			return down;
		}

		public boolean wallRight() {
			return right;
		}

		public boolean wallLeft() {
			return left;
		}

		public Polyhedra getRepresentation(PVector orig, float size) {
			Random rnd = new Random();
			float origX = orig.x;
			float origY = orig.y;
			float origZ = orig.z;
			Polyhedra rep = new Polyhedra();
			PolyhedraFace wall;
			if (down) {
				wall = new PolyhedraFace(rnd.nextInt());
				wall.addVertex(origX, origY, origZ);
				wall.addVertex(origX + size, origY, origZ);
				wall.addVertex(origX + size, origY, origZ + size);
				wall.addVertex(origX, origY, origZ + size);
				rep.addFace(wall);
			}
			if (left) {
				wall = new PolyhedraFace(rnd.nextInt());
				wall.addVertex(origX, origY, origZ);
				wall.addVertex(origX, origY + size, origZ);
				wall.addVertex(origX, origY + size, origZ + size);
				wall.addVertex(origX, origY, origZ + size);
				rep.addFace(wall);
			}
			if (right) {
				wall = new PolyhedraFace(rnd.nextInt());
				wall.addVertex(origX + size, origY, origZ);
				wall.addVertex(origX + size, origY + size, origZ);
				wall.addVertex(origX + size, origY + size, origZ + size);
				wall.addVertex(origX + size, origY, origZ + size);
				rep.addFace(wall);
			}
			if (up) {
				wall = new PolyhedraFace(rnd.nextInt());
				wall.addVertex(origX, origY + size, origZ);
				wall.addVertex(origX + size, origY + size, origZ);
				wall.addVertex(origX + size, origY + size, origZ + size);
				wall.addVertex(origX, origY + size, origZ + size);
				rep.addFace(wall);
			}
			return rep;
		}

	}

}
